package com.falcon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.falcon.core.utils.BeanUtils;
import com.falcon.domain.dos.QuestionOptionDO;
import com.falcon.domain.po.QuestionOptionPO;
import com.falcon.domain.req.QuestionOptionQuery;
import com.falcon.domain.req.QuestionOptionRequest;
import com.falcon.mapper.IQuestionOptionMapper;
import com.falcon.service.IQuestionOptionService;
import com.falcon.mvc.domain.BasePager;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swan.mybatis.condition.Condition;
import com.swan.mybatis.condition.OpType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class QuestionOptionService implements IQuestionOptionService {

    @Autowired
    private IQuestionOptionMapper questionOptionMapper;

    @Override
    public Long save(QuestionOptionRequest request){
        QuestionOptionPO questionOptionPO = BeanUtil.copyProperties(request, QuestionOptionPO.class);

        this.questionOptionMapper.insertNotNull(questionOptionPO);

        return questionOptionPO.getId();
    }

    @Override
    public Boolean update(Long id, QuestionOptionRequest request){

        QuestionOptionPO questionOptionPO = BeanUtil.copyProperties(request, QuestionOptionPO.class);
        questionOptionPO.setId(id);

        return this.questionOptionMapper.updateNotNull(questionOptionPO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.questionOptionMapper.deleteById(id);
    }

    @Override
    public QuestionOptionDO queryById(Long id){

        QuestionOptionPO questionOptionPO = this.questionOptionMapper.selectById(id);

        QuestionOptionDO questionOptionDO = BeanUtil.copyProperties(questionOptionPO, QuestionOptionDO.class);

        return questionOptionDO;
    }


    @Override
    public List<QuestionOptionDO> queryList(QuestionOptionQuery request){

        Condition condition = request.toCondition();

        List<QuestionOptionPO> poList = this.questionOptionMapper.selectList(condition);

        List<QuestionOptionDO> doList = BeanUtil.copyToList(poList, QuestionOptionDO.class);

        return doList;
    }

    @Override
    public BasePager<QuestionOptionDO> queryPager(QuestionOptionQuery request){


        PageInfo<QuestionOptionPO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.questionOptionMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, QuestionOptionDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.questionOptionMapper.deleteByIds(ids);
    }

    @Override
    public void deleteByQuestionIds(List<Long> questionIds) {

        Condition condition = Condition.newInstance()
                .and(QuestionOptionPO.Fields.questionId, OpType.in, questionIds);

        this.questionOptionMapper.deleteList(condition);

    }

    @Override
    public void batchSave(Long questionId, List<QuestionOptionRequest> optionRequestList) {
        // 保存子表
        List<QuestionOptionPO> optionPOS = BeanUtils.copyToList(optionRequestList, QuestionOptionPO.class);
        for (QuestionOptionPO optionPO : optionPOS) {
            optionPO.setQuestionId(questionId);
            questionOptionMapper.insertNotNull(optionPO);
        }
    }


}

